<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Feb 9, 2013
 * Time: 7:53:07 PM
 * To change this template use File | Settings | File Templates.
 */

class TldController extends BaseController {
    function actionTest() {

    }

    function actionCardList() {
        $this->render('cardList');
    }

    function actionAddCard() {

        if (isset($_GET['id'])) {
            $lotId = $_GET['id'];
            $tld = new Tld();
            $lotList = $tld->GetLotListMenu();
            $this->render('addCard', array('lotID' => $lotId, 'success' => true, 'lotList' => $lotList));
        } else {
            $this->render('addCard', array('success' => false));
        }

    }

    function actionCardSearch() {
        $this->render('search');
    }

    function actionAddHolder() {

        if (isset($_GET['id'])) {
            $lotId = $_GET['id'];
            $tld = new Holder();
            $lotList = $tld->GetLotListMenu();
            $this->render('addHolder', array('lotID' => $lotId, 'success' => true, 'lotList' => $lotList));
        } else {
            $this->render('addHolder', array('success' => false));
        }

    }

    function actionHolder() {
        $this->render('holderList');
    }

    function actionPass() {
        $tld = new Tld();
        $lotList = $tld->GetLotListMenu();
        $this->render('pass', array('lotList' => $lotList));
    }

    function actionUsedtld() {
        $this->render('usedtld');
    }

    function actionCheckCard() {
        $tld = new Tld();
        $id = $_POST['cardId'];
        $_message = $tld->validateCardId($id);
        $data = array('message' => $_message);
        echo json_encode($data);
    }

    function actionCheckHolder() {
        $tld = new Holder();
        $id = $_POST['cardId'];
        $_message = $tld->validateCardId($id);
        $data = array('message' => $_message);
        echo json_encode($data);
    }

    function actionGetTldList() {
        $lotId = $_POST['lotId'];
        $code = $_POST['code'];
        $tldCard = new TldCard();
        $data = $tldCard->getTldCardGrid($lotId, $code);
        echo json_encode($data);
    }

    function actionGetEmpCardList() {
        $insId = $_POST['instituteId'];
        $secId = $_POST['sectionId'];

        $empCard = new TldCard();
        $data = $empCard->getEmpTldCardList($insId, $secId);
        echo json_encode($data);
    }

    function actionGetCardInfoForDose() {
        $cardId = $_POST['cardId'];
        $card = new TldCard();
        $data = $card->getCardInfoForDose($cardId);

        echo json_encode($data);
    }

    function actionGetCardInfoForCollect() {
        $cardId = $_POST['cardId'];
        $card = new TldCard();
        $data = $card->getCardInfoForCollect($cardId);

        echo json_encode($data);
    }

    function actionGetCardInfo() {
        $cardId = $_POST['cardId'];
        $card = new TldCard();
        $data = $card->getCardInfo($cardId);

        echo json_encode($data);
    }

    function actionUpdateCard() {
        $id = $_POST['id'];
        $qaCard = $_POST['qa'];
        $brCard = $_POST['br'];
        $dosimeterCard = $_POST['dosimeter'];
        $performanceTestPass = $_POST['performanceTest'];
        $tldCard = new TldCard();
        $i = $tldCard->updateCard($id, $performanceTestPass, $qaCard, $brCard);
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly added new Section ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly insert ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed !";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated Section information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update ! ";
                $_type = "error";
            }
        }
        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }

    function actionUpdateEmpCardList() {
        $id = $_POST['id'];
        $list1 = $_POST['list1'];
        $list2 = $_POST['list2'];
        $institute = $_POST['instituteId'];
        $section = $_POST['sectionId'];
        $bgCard1 = $_POST['bg_card1'];
        $bgCard2 = $_POST['bg_card2'];
        if ($section > 0) {
            $instituteID = $section;
        } else {
            $instituteID = $institute;
        }
        $tldCard = new TldCard();
        $i = $tldCard->updateEmpCardList($id, $list1, $list2, $instituteID, $bgCard1, $bgCard2);
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly added new Section ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly insert ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed !";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated Section information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update ! ";
                $_type = "error";
            } else {
                $_message = $i . " is Not a valid TLD Card ";
                $_type = "error";
            }
        }
        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }

    function actionAddTldCard() {
        if ($this->userPermission["TLD.SAVE"] == 0) //check user privilege
            $this->urlRedirect('home');
        $tldCard = new TldCard();
        $tldCard->code = $_POST['cardId'];
        $tldCard->lotId = $_POST['id'];
        $i = $tldCard->save();
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly added new Section ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly insert ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed !";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated Section information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update ! ";
                $_type = "error";
            }
        }
        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }

    function actionAddTldHolder() {
        if ($this->userPermission["TLD.SAVE"] == 0) //check user privilege
            $this->urlRedirect('home');
        $tldCard = new TldHolder();
        $tldCard->code = $_POST['cardId'];
        $tldCard->lotId = $_POST['id'];
        $i = $tldCard->save();
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly added new Section ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly insert ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed !";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated Section information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update ! ";
                $_type = "error";
            }
        }
        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }


    function actionAddTldLot() {
        if ($this->userPermission["TLD.SAVE"] == 0) //check user privilege
            $this->urlRedirect('home');
        $tldLot = new Tld();
        $tldLot->lotName = $_POST["lotName"];
        $tldLot->type = $_POST["type"];
        $tldLot->price = $_POST["price"];
        $tldLot->material = $_POST["material"];
        $tldLot->supplier = $_POST["supplier"];
        $tldLot->specification = $_POST["specification"];
        $tldLot->quantity = $_POST["quantity"];
        $tldLot->receivedDate = $_POST["receivedDate"];
        $tldLot->codeRange = $_POST["codeRange"];
        $tldLot->id = $_POST["id"];
        $i = $tldLot->save();
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly added new Section ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly insert ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed !";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated Section information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update ! ";
                $_type = "error";
            }
        }
        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }

    function actionAddHolderLot() {
        if ($this->userPermission["TLD.SAVE"] == 0) //check user privilege
            $this->urlRedirect('home');
        $tldLot = new Holder();
        $tldLot->lotName = $_POST["lotName"];
        $tldLot->color = $_POST["color"];
        $tldLot->price = $_POST["price"];
        $tldLot->material = $_POST["material"];
        $tldLot->supplier = $_POST["supplier"];
        $tldLot->specification = $_POST["specification"];
        $tldLot->quantity = $_POST["quantity"];
        $tldLot->receivedDate = $_POST["receivedDate"];
        $tldLot->codeRange = $_POST["codeRange"];
        $tldLot->id = $_POST["id"];
        $i = $tldLot->save();
        if (is_bool($i)) {
            if ($i) {
                $_message = "Successsfuly added new Section ! ";
                $_type = "success";
            } else {
                $_message = "Unsuccesssfuly insert ! ";
                $_type = "error";
            }

        } else {
            if ($i == -1) {
                $_message = "No data was changed !";
                $_type = "info";
            } else if ($i == 1) {
                $_message = "Successsfuly updated Section information ! ";
                $_type = "success";
            } else if ($i == 0) {
                $_message = " Unsuccesssfuly update ! ";
                $_type = "error";
            }
        }
        $data = array('message' => $_message, 'type' => $_type);
        echo json_encode($data);
    }


    function actionGridCardList() {
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;

        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("lot_name", "type", "code_range", "received_date");

        $tld = new Tld();
        $xml = $tld->getGrid($posStart, $count, $fields[$_GET["orderBy"]], $_GET["direction"]);
        //$xml="this is test";
        header("Content-type:text/xml");
        print $xml;
    }

    function actionGridHolderList() {
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;

        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("lot_name", "type", "code_range", "received_date");

        $tld = new Holder();
        $xml = $tld->getGrid($posStart, $count, $fields[$_GET["orderBy"]], $_GET["direction"]);
        //$xml="this is test";
        header("Content-type:text/xml");
        print $xml;
    }

    function actionGridTld() {
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["code"]))
            $code = $_GET['code'];
        else
            $code = "";
        if (isset($_GET["lotId"]))
            $lotId = $_GET['lotId'];
        else
            $lotId = "";
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;

        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("id", "lot_id", "code");

        $tld = new Tld();
        $xml = $tld->getGridCard($code, $lotId, $posStart, $count, $fields[$_GET["orderBy"]], $_GET["direction"]);
        //$xml="this is test";
        header("Content-type:text/xml");
        print $xml;
    }

    function actionGridHolder() {
        error_reporting(E_ALL ^ E_NOTICE);
        ini_set('max_execution_time', 600);
        if (isset($_GET["code"]))
            $code = $_GET['code'];
        else
            $code = "";
        if (isset($_GET["lotId"]))
            $lotId = $_GET['lotId'];
        else
            $lotId = "";
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;

        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";
        $fields = array("id", "lot_id", "code");

        $tld = new Holder();
        $xml = $tld->getGridHolder($code, $lotId, $posStart, $count, $fields[$_GET["orderBy"]], $_GET["direction"]);
        //$xml="this is test";
        header("Content-type:text/xml");
        print $xml;
    }

}
